package com.spzx.product.service.impl;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.vo.CategoryBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryBrandServiceImpl implements CategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Override
    public List<CategoryBrandVo> selectList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectList(categoryBrand);
    }

    @Override
    public CategoryBrandVo getInfo(Long id) {
        CategoryBrandVo vo = categoryBrandMapper.getInfo(id);
        List<Long> categoryIdList =new ArrayList<>();
        categoryIdList.add(vo.getCategory1Id());
        categoryIdList.add(vo.getCategory2Id());
        categoryIdList.add(vo.getCategoryId());
        vo.setCategoryIdList(categoryIdList);
        return vo;
    }

    @Override
    public void save(CategoryBrand categoryBrand) {
        try {
            categoryBrandMapper.save(categoryBrand);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("该分类已加该品牌");
        }
    }

    @Override
    public void update(CategoryBrand categoryBrand) {
        try {
            categoryBrandMapper.update(categoryBrand);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("该分类已加该品牌");
        }
    }

    @Override
    public void delete(Long[] ids) {
        categoryBrandMapper.delete(ids);
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        List<Brand> list = categoryBrandMapper.selectBrandListByCategoryId(categoryId);
        return list;
    }
}
